package com.fenghongzhang.lib_core.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.fenghongzhang.lib_core.R;

public class FengTextView extends ViewGroup {

    private Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    String string;
    public FengTextView(Context context) {
        this(context, null);
    }

    //xml
    public FengTextView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.FengTextView);
        string = typedArray.getString(R.styleable.FengTextView_fengtext);
        typedArray.getBoolean(R.styleable.FengTextView_fengsize,true);
        typedArray.close();
    }

    public FengTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        this(context, attrs, defStyleAttr,0);

    }

    public FengTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init();
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        getChildAt(0).layout(0,0,200,200);
        getChildAt(1).layout(0,200,200,400);
    }


    private void init(){

    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int wMode = MeasureSpec.getMode(widthMeasureSpec);
        int specSize = MeasureSpec.getSize(widthMeasureSpec);

        var result = 100;
        switch (wMode) {
            case MeasureSpec.UNSPECIFIED:
                result = specSize;
                break;
            case MeasureSpec.AT_MOST:
                result = 200;
                break;
            case MeasureSpec.EXACTLY:
                result = specSize;
                break;
        }

        setMeasuredDimension(MeasureSpec.makeMeasureSpec(result,MeasureSpec.AT_MOST),MeasureSpec.makeMeasureSpec(result,wMode));
    }



    @Override
    protected void onDraw(@NonNull Canvas canvas) {
        super.onDraw(canvas);
        mPaint.setTextSize(30);
         canvas.drawText(string,50,50,mPaint);

    }
}
